package com.xoa.service.party;


import com.xoa.dao.party.EnDisputeMapper;
import com.xoa.model.party.EnDispute;
import com.xoa.model.users.Users;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.newwrapper.BaseWrapper;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 矛盾纠纷
 * Created by gsb on 2018/1/10.
 */
@Service
public class EnDisputeService {

    @Resource
    private EnDisputeMapper enDisputeMapper;
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午13:10:51
     * 类介绍  :   查找登陆人的信息
     * 构造参数:
     *
     */
    public ToJson<EnDispute> queryUser(HttpServletRequest request){
        ToJson<EnDispute> json = new ToJson<>();
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            EnDispute enDispute = new EnDispute();
            enDispute.setUsers(user);
            json.setObject(enDispute);
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 上午10:10:51
     * 类介绍  :   新建
     * 构造参数:
     *
     */
    public ToJson<EnDispute> insertSelective(EnDispute enDispute, HttpServletRequest request){
        ToJson<EnDispute> json = new ToJson<>();
        try {
          enDisputeMapper.insertSelective(enDispute);
          json.setMsg("ok");
          json.setFlag(0);

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10  下午13:10:51
     * 类介绍  :   删除
     * 构造参数:
     *
     */
    public ToJson<EnDispute> deleteByPrimaryKey(Integer cid){
        ToJson<EnDispute> json = new ToJson<>();
        try {
            enDisputeMapper.deleteByPrimaryKey(cid);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10  下午13:50:51
     * 类介绍  :   修改
     * 构造参数:
     *
     */
    public ToJson<EnDispute> updateByPrimaryKeySelective(EnDispute enDispute){
        ToJson<EnDispute> json = new ToJson<>();
        try {
            enDisputeMapper.updateByPrimaryKeySelective(enDispute);
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10  下午14:00:51
     * 类介绍  :  详情
     * 构造参数:
     *
     */
    public ToJson<EnDispute> selectById(Integer cid){
        ToJson<EnDispute> json = new ToJson<>();
        try {
            EnDispute enDispute = enDisputeMapper.selectById(cid);
            json.setObject(enDispute);
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10  下午14:30:51
     * 类介绍  :  列表
     * 构造参数:
     *
     */
    public ToJson<EnDispute> queryList(EnDispute enDispute, Integer page, Integer pageSize,
                                       boolean useFlag, HttpServletRequest request, HttpServletResponse response, String export){
        ToJson<EnDispute> json = new ToJson<>();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            if(useFlag){
                map.put("page", pageParams);
            }
            map.put("enDispute",enDispute);
            List<EnDispute> enDisputeList = enDisputeMapper.selectList(map);
            json.setObj(enDisputeList);
            json.setTotleNum(pageParams.getTotal());
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
    * @Author 程叶同
    * @Description 导出
    * @Date 14:08 2018/3/6
    */

    public BaseWrapper outPutFile(HttpServletRequest request, HttpServletResponse response){
        BaseWrapper baseWrapper=new BaseWrapper();
        Map<String,Object> map=new HashMap<String,Object>();
        List<EnDispute> enDisputeList = enDisputeMapper.selectList(map);
        /*if(ids!=null&&ids!=""){
            String[] id=ids.split(",");
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("id",id);
            illegalrentgroups=illegalrentgroupMapper.selectList(map);
        }*/

        try {

            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("矛盾纠纷排查表", 9);
            String[] secondTitles = {"社区名称","矛盾纠纷名称","报送时间","报送人","矛盾纠纷内容","矛盾纠纷处理状态","处理级别","调节次数"};
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            // String[] beanProperty = {user.getDep().getDeptName(),"userName","userPrivName", "roleAuxiliaryName","online","sex","online","telNoDept","telNoDept","departmentPhone","email"};
            String[] beanProperty = {"deptName","disputeName","submitTime","userName","disputeContent","disputeStaste",
                    "treatLevel","mediationNum"};

            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, enDisputeList, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "矛盾纠纷排查.xls";
            filename = FileUtils.encodeDownloadFilename(filename,
                    request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition",
                    "attachment;filename=" + filename);
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return  baseWrapper;
    }
}
